package com.wsl.service;


import org.apache.commons.io.IOUtils;
import org.springframework.beans.factory.BeanNameAware;
import org.springframework.context.ResourceLoaderAware;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.stereotype.Service;

import java.io.IOException;

/**
 * @Author:WangShuliang
 * @Description:
 * 实现BeanNameAware，ResourceLoaderAware接口，获得Bean名称和资源加载的服务
 *  实现  ResourceLoaderAware 需要重写setResourceLoader
 *  实现 BeanNameAware 需要重写setBeanName方法
 * @Date:Created in 16:20 2017/11/24
 * @Modified by:
 */
@Service
public class AwareService implements BeanNameAware,ResourceLoaderAware {
    private  String  beanName;
    private ResourceLoader loader;

    @Override
    public void setBeanName(String s) {
        this.beanName=s;
    }

    @Override
    public void setResourceLoader(ResourceLoader resourceLoader) {
        this.loader=resourceLoader;
    }

    public void outputResult() throws IOException {
        System.out.println("Bean的名称为:"+beanName);
        Resource  resource = loader.getResource("classpath:zym.txt");
        System.out.println("加载的文件的内容为"+ IOUtils.toString(resource.getInputStream()));
    }
}